﻿using  log4net.Appender;
using  log4net.Core;
using  System;

namespace  NDK.Log4Net
{
    public enum LogLevel
    {
        Debug = 1,
        Info,
        Warn,
        Error,
        Fatal
    }
    public class Log
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger("TraceLogger");

        /// <summary>
        /// 构造一个Logger
        /// </summary>
        /// <param name="location">出错位置</param>
        public Log(string location)
        {
            logger = log4net.LogManager.GetLogger(location);
            RollingFileAppender fileAppender = new RollingFileAppender();
            fileAppender.File = @"Log\";
            fileAppender.AppendToFile = true;
            fileAppender.MaxSizeRollBackups = 10;
            fileAppender.RollingStyle = RollingFileAppender.RollingMode.Composite;
            fileAppender.MaximumFileSize = "10MB";
            fileAppender.DatePattern = "yyyy-MM-dd'.log'";
            fileAppender.StaticLogFileName = false; 
            //日志输出格式：2020-03-06 20:35:54 [日志级别] [运行时间] [出错位置] [行号]  日志信息
            fileAppender.Layout = new log4net.Layout.PatternLayout(@"%d{yyyy-MM-dd HH:mm:ss}  [%-5p]  [%r] [%logger] [%L]  %m%n");
            
            log4net.Repository.Hierarchy.Logger lg = logger.Logger as log4net.Repository.Hierarchy.Logger;
            lg.AddAppender(fileAppender);
        }

        public void Debug(string str)
        {
            logger.Debug(str);
        }

        public void Info(string str)
        {
            logger.Info(str);
        }
        public void Warn(string str)
        {
            logger.Warn(str);
        }
        public void Error(string str)
        {
            logger.Error(str);
        }
        public void Fatal(string str)
        {
            logger.Fatal(str);
        }

        /// <summary>
        /// 更新日志等级
        /// </summary>
        /// <param name="level"></param>
        public static void UpdateLogLevel(LogLevel logLevel)
        {
            try
            {

                Level level = Level.Debug;//log4net 自带的类
                switch (logLevel)
                {
                    case LogLevel.Debug:
                        {
                            level = Level.Debug;
                            break;
                        }
                    case LogLevel.Info:
                        {
                            level = Level.Info;
                            break;
                        }
                    case LogLevel.Warn:
                        {
                            level = Level.Warn;
                            break;
                        }
                    case LogLevel.Error:
                        {
                            level = Level.Error;
                            break;
                        }
                    case LogLevel.Fatal:
                        {
                            level = Level.Fatal;
                            break;
                        }
                    default:
                        {
                            level = Level.Debug;
                            break;
                        }
                }

                log4net.Repository.Hierarchy.Logger lg = logger.Logger as log4net.Repository.Hierarchy.Logger;
                lg.Level = level;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }

        }
    }
}
